package com.gy.assistant.ui.chat.group.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import im.zego.zim.entity.ZIMGroupMemberInfo;
import com.gy.assistant.R;

public class GroupOwnerTransferAdapter extends RecyclerView.Adapter {
    public List<ZIMGroupMemberInfo> groupMemberInfoList = new ArrayList<>();
    public String selectedMember;
    private CompoundButton previewCheckBox;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_member_selected, parent, false);
        return new GroupOwnerTransferAdapter.GroupMemberSelectedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final GroupOwnerTransferAdapter.GroupMemberSelectedViewHolder groupMemberSelectedViewHolder = (GroupOwnerTransferAdapter.GroupMemberSelectedViewHolder) holder;

        ZIMGroupMemberInfo groupMemberInfo = groupMemberInfoList.get(position);
        groupMemberSelectedViewHolder.setGroupMemberInfo(groupMemberInfo);

        groupMemberSelectedViewHolder.groupMemberSelectedBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedMember = groupMemberInfo.userID;
                    if (previewCheckBox != null) {
                        previewCheckBox.setChecked(false);
                    }
                    previewCheckBox = buttonView;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return groupMemberInfoList.size();
    }

    public void addAll(List<ZIMGroupMemberInfo> list) {
        if (list != null) {
            groupMemberInfoList.addAll(list);
            notifyDataSetChanged();
        }
    }

    class GroupMemberSelectedViewHolder extends RecyclerView.ViewHolder {

        CheckBox groupMemberSelectedBox;
        TextView groupMemberNameTextView;

        public GroupMemberSelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            groupMemberSelectedBox = itemView.findViewById(R.id.group_member_selected);
            groupMemberNameTextView = itemView.findViewById(R.id.group_member_name);
        }

        public void setGroupMemberInfo(ZIMGroupMemberInfo info) {
            String name = info.memberNickname;
            if (info.memberNickname != null && !info.memberNickname.equals("")) {
                name = info.memberNickname;
            } else if (info.userName != null && !info.userName.equals("")) {
                name = info.userName;
            } else if (info.userID != null) {
                name = info.userID;
            }
            groupMemberNameTextView.setText(name);
        }
    }
}
